package com.banciyuan.bcywebview.biz.detail.goods;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.activity.BaseActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import de.greenrobot.daoexample.model.DetailGoods;
import de.greenrobot.daoexample.model.PhoneShell;

/**
 * Created by hy on 2015/6/29.
 */
public class GoodsShellImageActivity extends BaseActivity {

    private List<Fragment> mList = new ArrayList<>();
    private HashMap<String, Integer> map = new HashMap<>();
    private DetailGoods detailGoods;
    private ViewPager pager;
    private TextView tvPage;
    private TextView tvName;
    private String decrption;
    private int totalPage;
    private int index;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goodshell);
        initArgs();
        getFragment();
        initUi();
    }

    private void getFragment() {
        for (int i = 0; i < detailGoods.getPhone_shells().size(); i++) {
            GoodsShellFragment fragment = new GoodsShellFragment();
            Bundle bundle = new Bundle();
            bundle.putString("cover_url", detailGoods.getPhone_shells().get(i).getPath());
            if (detailGoods.getMulti().size() >= 2)
                bundle.putString("bottom_url", detailGoods.getMulti().get(1).getPath());
            fragment.setArguments(bundle);
            mList.add(fragment);
        }
    }

    @Override
    protected void initArgs() {
        Intent intent = getIntent();
        decrption = intent.getStringExtra("decrption");
        detailGoods = (DetailGoods) intent.getSerializableExtra("detailGoods");
        for (PhoneShell p : detailGoods.getPhone_shells()) {
            map.put(p.getDescription(), detailGoods.getPhone_shells().indexOf(p));
        }
        if (map.get(decrption) != null) {
            index = map.get(decrption);
        } else {
            index = 0;
        }
    }


    @Override
    protected void initUi() {
        tvName = (TextView) findViewById(R.id.tv_name);
        pager = (ViewPager) findViewById(R.id.pager);
        tvPage = (TextView) findViewById(R.id.tv_page);
        pager.setAdapter(new MyAdapter(getSupportFragmentManager()));
        pager.addOnPageChangeListener(new MyOnPageListener());
        pager.setCurrentItem(index);
        totalPage = detailGoods.getPhone_shells().size();
        int newPosition = index + 1;
        tvPage.setText(newPosition + "/" + totalPage);
        if (detailGoods.getPhone_shells().size() > 0 && index < detailGoods.getPhone_shells().size())
            tvName.setText(detailGoods.getPhone_shells().get(index).getDescription());
    }

    private class MyOnPageListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            index = position;
            position = position + 1;
            tvPage.setText(position + "/" + totalPage);
            if (detailGoods.getPhone_shells().size() > 0 && index < detailGoods.getPhone_shells().size())
                tvName.setText(detailGoods.getPhone_shells().get(index).getDescription());
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

    class MyAdapter extends FragmentPagerAdapter {

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mList.get(position);
        }

        @Override
        public int getCount() {
            return mList.size();
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {

    }
}
